package com.example.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.R;
import com.example.demo.viewmodel.PhoneViewModel;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.demo.databinding.ActivityPhoneBinding;

import java.util.Objects;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PhoneActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "PhoneActivity";

    private ActivityPhoneBinding binding;

    private PhoneViewModel phoneViewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityPhoneBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        phoneViewModel = new ViewModelProvider(this).get(PhoneViewModel.class);
        setDataBinding();

        binding.phoenSearch.setOnClickListener(this);

        setJump();
    }

    private void setJump(){
        binding.jumpToAstro.setOnClickListener(view -> {
            startActivity(new Intent(this, AstroActivity.class));
        });
        binding.jumpToHis.setOnClickListener(view -> {
            startActivity(new Intent(this, DayInHistoryActivity.class));
        });
        binding.jumpToGold.setOnClickListener(view -> {
            startActivity(new Intent(this, GoldActivity.class));
        });
        binding.jumpToNews.setOnClickListener(view -> {
            startActivity(new Intent(this, NewsListActivity.class));
        });
        binding.jumpToPhone.setOnClickListener(view -> {
            startActivity(new Intent(this, PhoneActivity.class));
        });
    }

    private void setDataBinding() {
        phoneViewModel.getUserLiveData().observe(this, data->{
            binding.phoneNumber.setText(data.getNumber());
            binding.phoneProvince.setText(data.getProvince());
            binding.phoneCity.setText(data.getCity());
            binding.phoneCompany.setText(data.getCompany());
            binding.phoneCardType.setText(data.getCardtype());
            binding.phoneAreaCode.setText(data.getAreacode());
        });
    }

    private void loadInfo() {
        phoneViewModel.loadData(binding.phoenNumberInput.getText().toString());
    }

    @Override
    public void onClick(View view) {
        if(view == binding.phoenSearch){
            loadInfo();
        }
    }
}